import 'dart:convert';

import 'package:flutter/material.dart';

import 'package:http/http.dart' as http;

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  List arr = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Stack(
          children: <Widget>[
            ListItem(arrList: this.arr),
            RaisedButton(
              onPressed: () async {
                var data =
                    await http.get('https://m.maoyan.com/ajax/movieOnInfoList');
                print(jsonDecode(data.body)['movieList']); // 将数据转化成json并取到
                setState(() {
                  arr = jsonDecode(data.body)['movieList'];
                });
              },
              child: Text('获取数据'),
            ),
          ],
        ),
      ),
    );
  }
}

// ignore: must_be_immutable
class ListItem extends StatelessWidget {
  List arrList = [];
  ListItem({Key key, @required this.arrList}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return new ListView.builder(
      itemCount: this.arrList.length,
      itemBuilder: (context, index) => Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(
            '评分${this.arrList[index]['sc']}',
          ), // 这是Map类型即{a: '213123', '324234'}取值需要mapObj['name']
          Text(
            '电影名${this.arrList[index]['nm']}',
          ),
          Text(
            '上映时间${this.arrList[index]['rt']}',
          ),
          Text(
            '场景${this.arrList[index]['version']}',
          ),
          Text(
            '演员${this.arrList[index]['star']}',
          ),
        ],
      ),
    );
  }
}
